package com.shopcart.service.impl;



import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.util.SqlUtils;
import com.shopcart.domain.UserAddress;
import com.shopcart.repository.UserAddressRepository;
import com.shopcart.service.UserAddressService;

@Service
@Transactional
public class UserAddressServiceImpl implements UserAddressService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(UserAddressServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private UserAddressRepository userAddressRepository;

	@Override
	public UserAddress selectMyAddress(String userId) {
		// TODO Auto-generated method stub
		Collection<UserAddress> list = userAddressRepository.findByUserId(userId, new Sort(Direction.DESC, "createTime"));
		if(list.size()>0) return list.iterator().next();
		return null;
	}

	@Override
	public int updateByPrimaryKeySelevtive(UserAddress address) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(address, true, " where id = '"+address.getId()+"'");
			return jdbcTemplate.update(sql);
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public void save(UserAddress address) {
		// TODO Auto-generated method stub
		userAddressRepository.save(address);
	}
	 
	

	
}
